<template>
  <!-- 组织结构组件-->
  <div>
    <div class="TOP">
      <div class="other-content">
        <div class="name">
          <h4>{{ item.name }}</h4>
        </div>
        <div class="user">{{ item.manager }}</div>
        <el-dropdown @command="commandEvent">
          <span class="el-dropdown-link">
            操作<i class="el-icon-arrow-down el-icon--right" />
          </span>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item command="add">添加子部门</el-dropdown-item>
            <el-dropdown-item
              v-if="!isTop"
              command="look"
            >查看部门</el-dropdown-item>
            <el-dropdown-item
              v-if="!isTop"
              command="del"
            >删除部门</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
    </div>
  </div>
</template>

<script>
import { companyDepartmentGETAPI } from '@/api'
export default {
  props: {
    item: {
      type: Object,
      default: () => {
        return {}
      }
    },
    isTop: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {}
  },
  methods: {
    commandEvent(command) {
      switch (command) {
        // 点击添加
        case 'add':
          this.$bus.$emit('addEvent', true, this.item, 'add')

          break
        // 点击查看
        case 'look':
          this.$bus.$emit('addEvent', true, this.item, 'edit')
          break
        // 点击删除
        case 'del':
          this.$confirm(
            `此操作将永久删除${this.item.name}部门, 是否继续?`,
            '提示',
            {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }
          )
            .then(async() => {
              this.$message({
                type: 'success',
                message: '删除成功!'
              })
              await companyDepartmentGETAPI(this.item.id)
              this.$emit('getData')
            })
            .catch(() => {
              this.$message({
                type: 'info',
                message: '已取消删除'
              })
            })
          break
      }
    }
  }
}
</script>

<style lang="scss">
.TOP {
  .other-content {
    display: flex;
    padding: 10px;
    align-items: center;
    .name {
      flex: 1;
    }
    .user {
      margin-right: 20px;
    }
    .el-dropdown-link {
      font-size: 17px;
      color: black;
    }
  }
}
</style>
